package services

import (
	"lottery/dao"
	"lottery/datasource"
	"lottery/models"
)

type CodeService interface {
	GetAll(page, size int) []models.LtCode
	CountAll() int64
	CountByGift(giftId int) int64
	Get(id int) (*models.LtCode, bool)
	Delete(id int) error
	Update(user *models.LtCode, columns []string) error
	Create(user *models.LtCode) error
	Search(giftId, page, size int) []models.LtCode
	NextUsingCode(giftId, codeId int) *models.LtCode
	UpdateByCode(data *models.LtCode, columns []string) error
	SearchAvailableCodeCount(giftId int) int
}

// 小写的，私有，内部使用，实现接口。
type codeService struct {
	dao *dao.CodeDao
}

//返回一个 CodeService
func NewCodeService() CodeService {
	return &codeService{
		dao: dao.NewCodeDao(datasource.InstanceDB()),
	}
}

// 实现接口函数, 直接调用dao中的实现。
func (s *codeService) GetAll(page, size int) []models.LtCode {
	data, _ := s.dao.GetAll(page, size)
	return data
}

func (s *codeService) Search(giftId, page, size int) []models.LtCode {
	return s.dao.Search(giftId, page, size)
}

// 查找所有可用优惠券的数量
func (s *codeService) SearchAvailableCodeCount(giftId int) int {
	return s.dao.SearchAvailableCodeCount(giftId)
}

// 查询单个奖品对应的 优惠券
func (s *codeService) CountByGift(giftId int) int64 {
	return s.dao.CountByGift(giftId)
}

func (s *codeService) CountAll() int64 {
	return s.dao.CountAll()
}

func (s *codeService) Get(id int) (*models.LtCode, bool) {
	data, ok := s.dao.Get(id)
	return data, ok
}

func (s *codeService) Update(data *models.LtCode, columns []string) error {
	return s.dao.Update(data, columns)
}

func (s *codeService) Create(data *models.LtCode) error {
	return s.dao.Create(data)
}

func (s *codeService) Delete(id int) error {
	return s.dao.Delete(id)
}

func (s *codeService) NextUsingCode(giftId, codeId int) *models.LtCode {
	return s.dao.NextUsingCode(giftId, codeId)
}

func (s *codeService) UpdateByCode(data *models.LtCode, columns []string) error {
	return s.dao.UpdateByCode(data, columns)
}
